home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / CMPLTPAS / SHELSORT.PAS < prev    next >
Pascal/Delphi Source File  |  1988-07-14  |  2KB  |  54 lines

  1. {->>>>ShellSort<<<<--------------------------------------------}
  2. {                                                              }
  3. { Filename : SHELSORT.SRC -- Last Modified 7/14/88             }
  4. {                                                              }
  5. { This is your textbook Shell sort on an array of key records, }
  6. { defined as the type show below:                              }
  7. {                                                              }
  8. {     KeyRec = RECORD                                          }
  9. {                Ref     : Integer;                            }
  10. {                KeyData : String30                            }
  11. {              END;                                            }
  12. {                                                              }
  13. {     From: COMPLETE TURBO PASCAL 5.0  by Jeff Duntemann       }
  14. {    Scott, Foresman & Co., Inc. 1988   ISBN 0-673-38355-5     }
  15. {--------------------------------------------------------------}
  16.  
  17. PROCEDURE ShellSort(VAR SortBuf : KeyArray; Recs : Integer);
  18.  
  19. VAR
  20.   I,J,K,L : Integer;
  21.   Spread  : Integer;
  22.  
  23.  
  24. PROCEDURE KeySwap(VAR RR,SS : KeyRec);
  25.  
  26. VAR
  27.   T : KeyRec;
  28.  
  29. BEGIN
  30.   T := RR;
  31.   RR := SS;
  32.   SS := T
  33. END;
  34.  
  35.  
  36. BEGIN
  37.   Spread := Recs DIV 2;       { First Spread is half record count  }
  38.   WHILE Spread > 0 DO         { Do until Spread goes to zero:      }
  39.     BEGIN
  40.       FOR I := Spread + 1 TO Recs DO
  41.         BEGIN
  42.           J := I - Spread;
  43.           WHILE J > 0 DO
  44.             BEGIN             { Test & swap across the array }
  45.               L := J + Spread;
  46.               IF SortBuf[J].KeyData <= SortBuf[L].KeyData THEN J := 0 ELSE
  47.                 KeySwap(SortBuf[J],SortBuf[L]);
  48.               J := J - Spread
  49.             END
  50.         END;
  51.       Spread := Spread DIV 2    { Halve Spread for next pass }
  52.     END
  53. END;
  54.